package com.xg.mina.message;

public class BasicHeader {
    /**
     * 消息头第一个字节前2位 00,01,10,11
     */
    private byte fmt;
    /**
     * 消息头第一个字节后6位 fmt 6 byte
     */
    private int fmtBytes;
    /**
     * 扩展流
     */
    private Integer chunkStreamId;

    public BasicHeader(int chunkStreamId) {
        if (chunkStreamId == 0 || chunkStreamId == 1 || chunkStreamId == 2) {
            System.err.println("控制消息");
        }
        this.chunkStreamId = chunkStreamId;
        if (chunkStreamId <= 63) {
            //单字节，不需要chunkStreamId
            fmtBytes = chunkStreamId;
            this.chunkStreamId = null;
        } else if (chunkStreamId <= 319) {
            //64-319
            fmtBytes = 0;
        } else {
            fmtBytes = 1;
            if (chunkStreamId > 65599) {
                throw new RuntimeException("chunkStreamId：" + chunkStreamId + " 不能大于65599");
            }
        }
    }

    public byte getFmt() {
        return fmt;
    }

    public void setFmt(byte fmt) {
        this.fmt = fmt;
    }

    public int getFmtBytes() {
        return fmtBytes;
    }

    public Integer getChunkStreamId() {
        return chunkStreamId;
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("BasicHeader{");
        sb.append("fmt=").append(fmt);
        sb.append(", fmtBytes=").append(fmtBytes);
        sb.append(", chunkStreamId=").append(chunkStreamId);
        sb.append('}');
        return sb.toString();
    }
}
